﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;

namespace _1stWpfPractice
{
    /// <summary>
    /// Interaction logic for StaffManagementPage.xaml
    /// </summary>
    public partial class StaffManagementPage : Window
    {
        BindingList<Staff> StaffList = new BindingList<Staff>();

        public StaffManagementPage()
        {
            InitializeComponent();
            grdStaffs.ItemsSource = StaffList;
        }
        
        private void Create(object sender, RoutedEventArgs e)
        {
            string name = txtName.Text;
            int age = int.Parse(txtAge.Text);
            Staff newStaff = null;
            switch ((this.cmbStaffType.SelectedItem as ComboBoxItem).Content.ToString())
            {
                case "Employee":
                    newStaff = new Employee(name, age);
                    break;
                case "Contractor":
                    newStaff = new Contractor(name, age);
                    break;
                case "Intern":
                    newStaff = new Intern(name, age);
                    break;
            }

            if (MessageBoxResult.OK == MessageBox.Show("You request to add a new item",
                "Are you sure?",
                MessageBoxButton.OKCancel))
            {
                StaffList.Add(newStaff);
                clearData();
            }
        }

        private void Update(object sender, RoutedEventArgs e)
        {
            string updateName = txtName.Text;
            int updateAge = int.Parse(txtAge.Text);


            if (MessageBoxResult.OK == MessageBox.Show("You request to update this item",
                "Are you sure?",
                MessageBoxButton.OKCancel))
            {
                (grdStaffs.SelectedItem as Staff).Name = updateName;
                (grdStaffs.SelectedItem as Staff).Age = updateAge;
            }
        }

        private void Delete(object sender, RoutedEventArgs e)
        {
            if (MessageBoxResult.OK == MessageBox.Show("Are you sure?",
            "Delete Files",
            MessageBoxButton.OKCancel))
            {
                StaffList.Remove((grdStaffs.SelectedItem as Staff));
                clearData();
            }
        }

        void clearData()
        {
            txtName.Text = string.Empty;
            txtAge.Text = string.Empty;
            cmbStaffType.SelectedIndex = 0;
        }

        private void grdStaffS_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
			// here is right, setup the callback method for grid of staffs
			if (grdStaffs.SelectedItem == null || grdStaffs.SelectedItem.ToString() == "{NewItemPlaceholder}") { return; }
			txtName.Text = (grdStaffs.SelectedItem as Staff).Name.ToString();
			txtAge.Text = (grdStaffs.SelectedItem as Staff).Age.ToString();
			cmbStaffType.SelectedIndex = (int)(grdStaffs.SelectedItem as Staff).StaffType;
        }

        private void grdStaffS_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
        {

        }

        private void cmbStaffType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}
